$.fn.pagingGenerator = function(option) {
	return new PagingGenerator(option, this);
}

var PagingGenerator = function(option, ele) {
	this.ele = ele;
	this.defaultData = {
		action: "",
		type:"POST",
		pagingTemplate: "",
		itemTemplate: "",
		pagingScript: null,
		itemScript: function($item,itemData){},
		page:"1",
		rows:"5",
		success: function(data) {
		},
		error: function(data) {
		}
	}

	if(option){
		$.extend(this.defaultData,option);
	}

	this.selectorMap = {
		searchForm: "[search-form]",
		filterForm: "[filter-form]",
		itemContainer: "[item-container]",
		pagingBarContainer: "[paging-bar-container]"
	}


	this.loadPagingTemplate();
	this.init();
	this.loadItemTemplate();
	this.load();
	
	if(this.defaultData.pagingScript&&this.defaultData.pagingScript!=null){
		this.defaultData.pagingScript(this,this.ele);
	}

	return this;
}

/**
 * 初始化
 */
PagingGenerator.prototype.init = function() {


	for(index in this.selectorMap) {
		this[index] = this.ele.find(this.selectorMap[index]);
	}


}
/**
 * 加载分页模板
 */
PagingGenerator.prototype.loadPagingTemplate = function() {

	var me = this;

	var url = this.defaultData.pagingTemplate;
	//console.info("paging-generator:" + url);
	$.ajax({
		url: url,
		type: "get",
		async: false,
		dataType: "html",
		success: function(data) {
			if(data != null) {
				me.ele.append(data);

			} else {
			}
		},
		error: function(data) {

		}
	})

}
/**
 * 加载列表模板
 */
PagingGenerator.prototype.loadItemTemplate = function() {

	var me = this;

	var url = this.defaultData.itemTemplate;
	$.ajax({
		url: url,
		type: "get",
		async: false,
		dataType: "html",
		success: function(data) {
			if(data != null) {
				me.itemTemplateStr = data;
			} else {
			}
		},
		error: function(data) {

		}
	})
}

/**
 * 加载数据
 */
PagingGenerator.prototype.loadJson = function(action,page,rows) {
	var me = this;

	var params ={
		pageNum:page,
		pageSize:rows,
	}
	if(this.searchForm&&this.searchForm.serializeArray){
		var searchParams={}
		var searchParamArray = this.searchForm.serializeArray();
		for(var i in searchParamArray){
			searchParams[searchParamArray[i].name]=searchParamArray[i].value;
		}
		$.extend(params,searchParams);
	}
	if(this.filterForm&&this.filterForm.serializeArray){
		var filterParams={}
		var filterParamArray = this.filterForm.serializeArray();
		for(var i in filterParamArray){
			filterParams[filterParamArray[i].name]=filterParamArray[i].value;
		}
		$.extend(params,filterParams);
	}
	//console.log(params);
	var ajaxOpt = {
		url: this.defaultData.action,
		async: false,
		dataType: "json",
		type:this.defaultData.type,
		data:params,
		success: function(data) {
			me.result = data;
			if(data.list) {
				me.dataMap = data.list;
				me.pagination=data;
				me.dataRows= data.list;
				if(me.defaultData.success) {
					me.defaultData.success(data);
				}
			} else {
				if(me.defaultData.success.error) {
					me.defaultData.success.error(data);
				}
			}
		},
		error: function(data) {
		}
	}

	$.ajax(ajaxOpt);
}

/**
 * 加载数据，模板并渲染模板
 */
PagingGenerator.prototype.load = function() {
	//console.log(this.defaultData.action);
	this.loadAction(this.defaultData.action,this.defaultData.page,this.defaultData.rows);
}
/**
 * 加载数据，模板并渲染模板
 */
PagingGenerator.prototype.refresh = function() {
	console.log(this.pagination);
	
	this.loadAction(this.defaultData.action,this.pagination.pageNum,this.pagination.pageSize);
}

/**
 * 加载数据，模板并渲染模板
 */
PagingGenerator.prototype.loadAction = function(action,page,rows) {
	var me = this;
	this.loadJson(action,page,rows);
	this.render();
	if(this.onFlip){
		this.onFlip(this.pagination);
	}
	if(this.defaultData.onload){
		this.defaultData.onload(this,this.ele);
	}
}

PagingGenerator.prototype.setAction = function(action) {

	this.defaultData.action = action;
}

PagingGenerator.prototype.setItemTeplate = function(itemTemplate) {

	this.defaultData.itemTemplate = itemTemplate;
}

/**
 * 渲染模板
 */
PagingGenerator.prototype.render = function() {
	var pattn = /\$\{(.*?)}/g;

	this.itemContainer.empty();

	var template = this.itemTemplateStr;

	for(var i = 0; i < this.dataRows.length; i++) {
		while((arr = pattn.exec(template)) != null) {

			var pre = template.substring(0, arr.index);

			var tail = template.substring(pattn.lastIndex, template.length);

			var expression = arr[1];

			var val = null;

			var filter = null;

			if(expression.indexOf("|") >= 0) {
				var filterStr = expression.split("|")[1];
				expression = expression.split("|")[0];
				filter = filters[filterStr];
			}

			val=this.dataRows[i][expression];

			if(val != null & val != "" && filter != null) {
				val = filters[filterStr](val);
			}
			template = pre + val + tail;

			pattn.lastIndex = 0;
		}

		var $item = $(template);
		var itemData=this.dataRows[i];
		this.defaultData.itemScript($item,itemData);


		this.itemContainer.append($item);
		template=this.itemTemplateStr;
	}



}
